// 错误信息
const MAIL_STATUS = {
    200: '成功',
    300: '超时',
    40000: '传入参数错误',
    40001: '邮箱账号不存在',
    40002: '邮箱账号已停用',
    40003: '邮箱渠道不存在',
    40004: '邮箱渠道已停用',
    40005: '邮件收件人列表为空',
    40006: '邮箱格式错误',
    40007: '附件传输key错误',
    40008: '附件不存在',
    40009: '访问阿里云oss失败'
}

// 中台发送信息接口
const BASE_URL = 'https://api.more-health.cn/msg/mail/sendMail'

// 发送模版配置
let templates = {
    pwd: {
        subject: '鲁班素材管理平台-密码',
        content: '<br/>欢迎来到鲁班素材管理平台，您的账号密码是 <span style="font-size:20px;color:#333;font-weight:600">__placeholder__</span>'
    }   
    // ...
}

/**
 * 参考中台邮件参数配置
 * https://wiki.miaomore.cn/pages/viewpage.action?pageId=28455957
 */
let mailOptions = {
    appId: 320701,
    appSecret: '9cbd8c61330b9df56543735b9484fe7b',
    sender: 'insurance@miao.cn',
    emails: [],
    ccs: [],
    sendType: 2,
    mailType: 2,
    subject: '',
    content: '',
    failStrategy: 2
    // attachments: [ //附件📎
    //     {
    //         attachName: 'xxx.zip',
    //         objectName: 'xxxxxxxxxxx'
    //     }
    // ]
};
const handleOtherParam = (type, other) => {
    if(type === 'pwd') {
        mailOptions.content = mailOptions.content.replace(/__placeholder__/i, other.pwd)
    }
    // do other type
}

/**
 * 
 * @param {发送对象} emails 
 * @param {邮件类型} type 
 * @param {额外参数} other 
 * @param {抄送对象} ccs 
 */
const send_email = async (emails, type, other={}, ccs=[]) => {
    mailOptions.emails = emails;
    if(ccs.length > 0) mailOptions.emails = ccs;
    mailOptions.subject = templates[type].subject;
    mailOptions.content = templates[type].content;
    handleOtherParam(type, other);
    
    try {
        return await $http._post(BASE_URL, mailOptions)
    } catch (error) {
        return error
    }
}

module.exports = send_email;
